package com.haojiang.mh.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.haojiang.mh.R;
import com.haojiang.mh.utils.SizeUtil;

/**
 * 四种睡眠状态下，达标状况（总宽度对应8小时睡眠）
 * <p>
 * Created by ZapFive on 2019-05-25.
 * <p>
 * wuzhuang@mirahome.me
 */
public class SleepStandardView extends View {

    public static final int DEEP_SLEEP = 0;
    public static final int LIGHT_SLEEP = 1;
    public static final int REM = 2;
    public static final int AWAKE = 3;
    //线、圆柱、达标框 画笔
    private Paint linePaint, columnPaint, standardPaint;
    private int width, height, defaultWidth, defaultHeight;//当前View宽高,默认宽高
    private int columnHeight, standardHeight, padding;//圆柱、达标框 高度,间隔
    private int columnColor, standardColor, lineColor;  //圆柱、达标框、线 颜色
    private int deepColor, lightColor, remColor, awakeColor;//深睡眠、浅睡眠、rem、清醒 颜色值
    private int standardLeft, standardRight;//达标对应范围 左、右 大小（分钟）
    private RectF rectF = new RectF();//矩形
    private boolean isDrawStandard = true;//是否绘制达标框
    private int sleepMinute;//睡眠分钟

    public SleepStandardView(Context context) {
        this(context, null);
    }

    public SleepStandardView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SleepStandardView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        standardLeft = 60;
        standardRight = 180;

        columnHeight = SizeUtil.dp2px(context, 5);
        standardHeight = SizeUtil.dp2px(context, 15);
        padding = SizeUtil.dp2px(context, 1);

        deepColor = getResources().getColor(R.color.purple_4);
        lightColor = getResources().getColor(R.color.purple_3);
        remColor = getResources().getColor(R.color.purple_2);
        awakeColor = getResources().getColor(R.color.grey_3);
        columnColor = deepColor;

        standardColor = getResources().getColor(R.color.green_light);
        lineColor = getResources().getColor(R.color.white) & 0x4CFFFFFF;

        defaultWidth = SizeUtil.dp2px(context, 150) + padding * 2;
        defaultHeight = SizeUtil.dp2px(context, 15) + padding * 2;

        linePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        linePaint.setStyle(Paint.Style.FILL);
        linePaint.setColor(lineColor);

        columnPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        columnPaint.setStyle(Paint.Style.FILL);

        standardPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        standardPaint.setStyle(Paint.Style.STROKE);
        standardPaint.setStrokeWidth(SizeUtil.dp2px(context, 1.5f));
        standardPaint.setColor(standardColor);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        width = w;
        height = h;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.drawLine(padding, height * 0.5f, width - padding, height * 0.5f, linePaint);

        if (sleepMinute > 0) {
            rectF.set(
                    padding,
                    height * 0.5f - columnHeight * 0.5f,
                    padding + sleepMinute * (width - padding * 2) / 480f,
                    height * 0.5f + columnHeight * 0.5f
            );
            columnPaint.setColor(columnColor);
            canvas.drawRoundRect(rectF, columnHeight * 0.5f, columnHeight * 0.5f, columnPaint);
        }

        if (!isDrawStandard) return;
        rectF.set(
                padding + standardLeft * (width - padding * 2) / 480f,
                height * 0.5f - standardHeight * 0.5f,
                padding + standardRight * (width - padding * 2) / 480f,
                height * 0.5f + standardHeight * 0.5f
        );
        canvas.drawRoundRect(rectF, standardHeight * 0.5f, standardHeight * 0.5f, standardPaint);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(
                getMeasuredLength(widthMeasureSpec, true),
                getMeasuredLength(heightMeasureSpec, false)
        );
    }

    private int getMeasuredLength(int length, boolean isWidth) {
        int specMode = MeasureSpec.getMode(length);
        int specSize = MeasureSpec.getSize(length);
        int size;
        int padding = isWidth ? getPaddingLeft() + getPaddingRight() : getPaddingTop() + getPaddingBottom();
        if (specMode == MeasureSpec.EXACTLY) {
            size = specSize;
        } else if (specMode == MeasureSpec.UNSPECIFIED) {
            size = isWidth ? defaultWidth : defaultHeight;
        } else {
            size = isWidth ? padding + defaultWidth : padding + defaultHeight;
            if (specMode == MeasureSpec.AT_MOST) {
                size = Math.min(size, specSize);
            }
        }
        return size;
    }

    /**
     * 更新value和状态
     *
     * @param sleepMinute 时长
     * @param type        类型
     */
    public void setSleepTimeAndType(int sleepMinute, int type) {
        this.sleepMinute = sleepMinute;
        this.isDrawStandard = (type != AWAKE);
        switch (type) {
            case DEEP_SLEEP:
                standardLeft = 60;
                standardRight = 180;
                columnColor = deepColor;
                break;
            case LIGHT_SLEEP:
                standardLeft = 180;
                standardRight = 300;
                columnColor = lightColor;
                break;
            case REM:
                standardLeft = 60;
                standardRight = 180;
                columnColor = remColor;
                break;
            case AWAKE:
                columnColor = awakeColor;
                break;
        }
        invalidate();
    }
}
